package com.sojson.util.properties.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.sojson.constant.Constant;
import com.sojson.util.StringUtil;
import com.sojson.util.properties.PropertiesUtilInterface;

/**
 * Properties工具类实现类
 * 
 * @author liu
 * @date 2021-06-29
 */
public class PropertiesUtilImpl implements PropertiesUtilInterface {

    private static PropertiesUtilImpl propertiesUtilsImpl;

    private PropertiesUtilImpl() {}

    /**
     * 读取resources下所有相同名字的properties文件，返回一个Properites对象
     * 
     * @param fileName properties文件名（文件必须要在resources文件夹下）
     * @return 得到的对象
     * @throws IOException
     */
    @Override
    public Properties getProperties(String fileName) throws IOException {
        EncodedResource encodedResource =
            new EncodedResource(new ClassPathResource(fileName), Constant.DEFAULT_ENCODING);
        return PropertiesLoaderUtils.loadProperties(encodedResource);
    }

    /**
     * 读取resources下所有相同名字的properties文件，返回一个Properites对象
     * 
     * @param fileName  properties文件名（文件必须要在resources文件夹下）
     * @param encoding  字符编码
     * @return 得到的对象
     * @throws IOException
     */
    @Override
    public Properties getProperties(String fileName, String encoding) throws IOException {
        return getProperties(new ClassPathResource(fileName), encoding);
    }

    /**
     * 根据properties文件输入流获取Properties对象
     * 
     * @param file 文件
     * @return 得到的Properties对象
     * @throws IOException
     */
    @Override
    public Properties getProperties(File file) throws IOException {
        return getProperties(file, Constant.DEFAULT_ENCODING);
    }

    /**
     * 根据properties文件输入流获取Properties对象
     * 
     * @param file 文件
     * @param encoding  字符编码
     * @return 得到的Properties对象
     * @throws IOException
     */
    @Override
    public Properties getProperties(File file, String encoding) throws IOException {
        Properties properties = new Properties();

        FileInputStream fileInputStream = null;
        InputStreamReader inputStreamReader = null;
        try {
            fileInputStream = new FileInputStream(file);
            inputStreamReader = new InputStreamReader(fileInputStream, encoding);
            properties.load(inputStreamReader);
        } finally {
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
            if (fileInputStream != null) {
                fileInputStream.close();
            }
        }

        return properties;
    }

    /**
     * 根据properties文件路径获取Properties对象
     * 
     * @param resource
     * @return 得到的Properties对象
     * @throws IOException
     */
    @Override
    public Properties getProperties(Resource resource) throws IOException {
        return getProperties(resource, Constant.DEFAULT_ENCODING);
    }

    /**
     * 根据properties文件路径获取Properties对象
     * 
     * @param resource
     * @param encoding  字符编码
     * @return 得到的Properties对象
     * @throws IOException
     */
    @Override
    public Properties getProperties(Resource resource, String encoding) throws IOException {
        EncodedResource encodedResource = new EncodedResource(resource, encoding);
        return PropertiesLoaderUtils.loadProperties(encodedResource);
    }

    /**
     * 根据properties文件输入流获取Properties对象
     * 
     * @param inputStream properties文件输入流
     * @return 得到的Properties对象
     * @throws IOException
     */
    @Override
    public Properties getProperties(InputStream inputStream) throws IOException {
        Properties properties = new Properties();
        properties.load(inputStream);
        return properties;
    }

    /**
     * 获取properites文件中的某个值
     * 
     * @param fileName properties文件名（文件必须要在resources文件夹下）
     * @param key 要获取的值的key
     * @return 得到的值
     * @throws IOException
     */
    @Override
    public String getValue(String fileName, String key) throws IOException {
        return getProperties(fileName).getProperty(key);
    }

    public static PropertiesUtilImpl getInstances() {
        if (StringUtil.isBlankObject(propertiesUtilsImpl)) {
            propertiesUtilsImpl = new PropertiesUtilImpl();
        }
        return propertiesUtilsImpl;
    }

}